<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      class="query-params-form"
    >
      <el-form-item label="菜单编码" prop="menuNumber">
        <el-input
          v-model="queryParams.menuNumber"
          placeholder="请输入菜单编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          placeholder="请输入菜单名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源单名称" prop="sourceName">
        <el-input
          v-model="queryParams.sourceName"
          placeholder="请输入源单名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标单名称" prop="targetName">
        <el-input
          v-model="queryParams.targetName"
          placeholder="请输入目标单名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['barcode:page:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['barcode:page:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['barcode:page:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['barcode:page:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="pageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="55" />
      <el-table-column label="菜单编号" align="center" prop="menuId" />
      <el-table-column label="菜单编码" align="center" prop="menuNumber" />
      <el-table-column label="菜单名称" align="center" prop="menuName" />
      <!-- <el-table-column label="一级菜单分组编码" align="center" prop="firstmenuGroupnumber" />
      <el-table-column label="一级菜单分组名称" align="center" prop="firstmenuGroupname" /> -->
      <el-table-column
        label="源单标识"
        align="center"
        prop="sourceIdentifier"
      />
      <el-table-column label="源单名称" align="center" prop="sourceName" />
      <el-table-column
        label="目标单标识"
        align="center"
        prop="targetIdentifier"
      />
      <el-table-column label="目标单名称" align="center" prop="targetName" />

      <el-table-column label="源单界面" align="center" width="90">
        <template #default="scope">
          <el-checkbox
            :true-label="1"
            :false-label="0"
            v-model="scope.row.enableSourceInterface"
          ></el-checkbox>
        </template>
      </el-table-column>

      <el-table-column label="操作界面" align="center" width="90">
        <template #default="scope">
          <el-checkbox
            :true-label="1"
            :false-label="0"
            v-model="scope.row.enableOperationInterface"
          ></el-checkbox>
        </template>
      </el-table-column>

      <el-table-column label="查询界面" align="center" width="90">
        <template #default="scope">
          <el-checkbox
            :true-label="1"
            :false-label="0"
            v-model="scope.row.enableQueryInterface"
          ></el-checkbox>
        </template>
      </el-table-column>

      <el-table-column label="草稿界面" align="center" width="90">
        <template #default="scope">
          <el-checkbox
            :true-label="1"
            :false-label="0"
            v-model="scope.row.enableDraftInterface"
          ></el-checkbox>
        </template>
      </el-table-column>

      <!-- <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建日期" align="center" prop="createdDate" />
      <el-table-column label="修改人" align="center" prop="modifiedBy" />
      <el-table-column label="修改日期" align="center" prop="modifiedDate" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="160"
      >
        <template #default="scope">
          <el-row :gutter="5">
            <el-col :span="12">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['barcode:page:edit']"
                >修改</el-button
              ></el-col
            >
            <el-col :span="12">
              <el-button
                link
                type="primary"
                icon="Setting"
                @click="handleSetting(scope.row)"
                v-hasPermi="['barcode:page:edit']"
                >配置</el-button
              ></el-col
            >
          </el-row>
          <el-row :gutter="5">
            <el-col :span="12">
              <el-button
                link
                type="primary"
                icon="CollectionTag"
                @click="handleField(scope.row)"
                v-hasPermi="['barcode:page:edit']"
                >字段</el-button
              ></el-col
            >
            <el-col :span="12">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['barcode:page:remove']"
                >删除</el-button
              ></el-col
            >
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改条码页面对话框 -->
    <el-dialog :title="title" v-model="open" width="530px" append-to-body>
      <el-form
        ref="pageRef"
        :model="form"
        :rules="rules"
        label-width="130px"
        class="edit-form"
      >
        <el-form-item label="菜单编号" prop="menuId">
          <el-input
            v-model="form.menuId"
            placeholder="请选择菜单编号"
            disabled
          />
          <el-button
            type="text"
            style="margin-left: 10px"
            @click="selectMenuList"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item label="菜单编码" prop="menuNumber">
          <el-input
            v-model="form.menuNumber"
            placeholder="请输入菜单编码"
            disabled
          />
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input
            v-model="form.menuName"
            placeholder="请输入菜单名称"
            disabled
          />
        </el-form-item>
        <el-form-item label="一级菜单分组编码" prop="firstmenuGroupnumber">
          <el-input
            v-model="form.firstmenuGroupnumber"
            placeholder="请输入一级菜单分组编码"
            disabled
          />
        </el-form-item>
        <el-form-item label="一级菜单分组名称" prop="firstmenuGroupname">
          <el-input
            v-model="form.firstmenuGroupname"
            placeholder="请输入一级菜单分组名称"
            disabled
          />
        </el-form-item>
        <el-form-item label="源单标识" prop="sourceIdentifier">
          <el-input
            v-model="form.sourceIdentifier"
            placeholder="请输入源单标识"
          />
        </el-form-item>
        <el-form-item label="源单名称" prop="sourceName">
          <el-input v-model="form.sourceName" placeholder="请输入源单名称" />
        </el-form-item>
        <el-form-item label="目标单标识" prop="targetIdentifier">
          <el-input
            v-model="form.targetIdentifier"
            placeholder="请输入目标单标识"
          />
        </el-form-item>
        <el-form-item label="目标单名称" prop="targetName">
          <el-input v-model="form.targetName" placeholder="请输入目标单名称" />
        </el-form-item>
        <el-form-item label="启用源单" prop="">
          <el-radio-group v-model="form.enableSourceInterface">
            <el-radio
              v-for="dict in barcode_show_hide"
              :key="dict.value"
              :value="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="启用操作" prop="">
          <el-radio-group v-model="form.enableOperationInterface">
            <el-radio
              v-for="dict in barcode_show_hide"
              :key="dict.value"
              :value="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="启用查询" prop="">
          <el-radio-group v-model="form.enableQueryInterface">
            <el-radio
              v-for="dict in barcode_show_hide"
              :key="dict.value"
              :value="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="启用草稿" prop="">
          <el-radio-group v-model="form.enableDraftInterface">
            <el-radio
              v-for="dict in barcode_show_hide"
              :key="dict.value"
              :value="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <select-Menu ref="selectMenuRef" @ok="handleImportMenu" />
  </div>
</template>

<script setup name="Page">
import {
  listPage,
  getPage,
  delPage,
  addPage,
  updatePage,
} from "@/api/barcode/page";

import selectMenu from "@/views/barcode/menu/select.vue";

const { proxy } = getCurrentInstance();
const router = useRouter();
const { barcode_show_hide } = proxy.useDict("barcode_show_hide");

const pageList = ref([]);
const open = ref(false);
const isAdd
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    menuNumber: null,
    menuName: null,
    sourceName: null,
    targetName: null,
  },
  rules: {
    id: [{ required: true, message: "ID不能为空", trigger: "blur" }],
    menuId: [{ required: true, message: "菜单编号不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询条码页面列表 */
function getList() {
  loading.value = true;
  listPage(queryParams.value).then((response) => {
    pageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    menuId: null,
    menuNumber: null,
    menuName: null,
    firstmenuGroupnumber: null,
    firstmenuGroupname: null,
    sourceIdentifier: null,
    sourceName: null,
    targetIdentifier: null,
    targetName: null,
    enableSourceInterface: 1,
    enableOperationInterface: 1,
    enableQueryInterface: 1,
    enableDraftInterface: 1,
    createdBy: null,
    createdDate: null,
    modifiedBy: null,
    modifiedDate: null,
  };
  proxy.resetForm("pageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加条码页面";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getPage(_id).then((response) => {
    form.value = response.data;
    form.value.enableSourceInterface = form.value.enableSourceInterface;
    form.value.enableOperationInterface = form.value.enableOperationInterface;
    form.value.enableQueryInterface = form.value.enableQueryInterface;
    form.value.enableDraftInterface = form.value.enableDraftInterface;
    open.value = true;
    title.value = "修改条码页面";
  });
}

function handleSetting(row) {
  router.push("/barcode/pageconfig/index/" + row.id);
}

function handleField(row) {
  router.push("/barcode/pagefield/index/" + row.id);
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pageRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updatePage(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPage(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除条码页面编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delPage(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "barcode/page/export",
    {
      ...queryParams.value,
    },
    `page_${new Date().getTime()}.xlsx`
  );
}

function selectMenuList() {
  proxy.$refs["selectMenuRef"].show();
}

function handleImportMenu(selectedRow) {
  console.log("handleImportMenu", selectedRow);
  if (selectedRow.length !== 1) {
    proxy.$modal.msgError("请选择一条数据!");
    return;
  }
  const _fieldRow = selectedRow[0];

  form.value.menuId = _fieldRow.id;
  form.value.menuNumber = _fieldRow.menuNumber;
  form.value.menuName = _fieldRow.menuName;
  form.value.firstmenuGroupnumber = _fieldRow.firstmenuGroupnumber;
  form.value.firstmenuGroupname = _fieldRow.firstmenuGroupname;

  proxy.$refs["selectMenuRef"].hide();
}

getList();
</script>

<style scoped>
.edit-form .el-input {
  width: 240px;
}
</style>
